package com.hyhy.hycloud.uaa.controller;

import com.hyhy.hycloud.commons.constant.HttpHeader;
import com.hyhy.hycloud.commons.dto.InvokeResult;
import com.hyhy.hycloud.uaa.security.SecurityUser;
import com.hyhy.hycloud.uaa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * UAA 用户相关REST API
 *
 * @author Xuegui Yuan
 * @date 2018-09-18
 */
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/user-info")
    public InvokeResult<?> getLoggedUserInfoByToken(@AuthenticationPrincipal SecurityUser securityUser,
                                                    @RequestHeader(name = HttpHeader.APP_ID, required = false) String appId,
                                                    @RequestParam(name = "withDetails", defaultValue = "true") Boolean withDetails,
                                                    @RequestParam(name = "withResources", defaultValue = "true") boolean withResources) {
        if (withDetails) {
            return InvokeResult.success(userService.getUserInfoBySecurityUser(appId, securityUser, withResources));
        }
        return InvokeResult.success(securityUser);
    }

    @GetMapping("/user-info/id")
    public InvokeResult<?> getLoggedUserInfoById(@RequestHeader(name = "X-User-Id") String userId,
                                                 @RequestHeader(name = HttpHeader.APP_ID, required = false) String appId,
                                                 @RequestParam(name = "withDetails", defaultValue = "true") Boolean withDetails,
                                                 @RequestParam(name = "withResources", defaultValue = "true") boolean withResources) {
        if (withDetails) {
            return InvokeResult.success(userService.getUserInfoByUserId(appId, userId, withResources));
        }
        return InvokeResult.success(userService.findUserDetailsByUserId(userId));
    }

}
